package com.mhkj.base.functionOffice.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.functionOffice.entity.FunctionOffice;
import com.mhkj.base.functionOffice.service.FunctionOfficeService;
import com.mhkj.base.rbac.role.entity.SystemRole;
import com.mics.core.common.componet.JsonHandler;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.common.componet.SimpleCode;
import com.mics.core.common.util.json.JsonUtils;
import com.mics.core.system.base.controller.BaseController;

/**
 * 职能部门 
 * @author mics
 * @date 2018年9月17日
 * @version 1.0
 */
@Controller
@RequestMapping("/functionOffice")
public class FunctionOfficeController extends BaseController<FunctionOffice>{
	@Resource
	private FunctionOfficeService functionOfficeService;
	@Resource
	private SimpleCode simpleCode;
	@Resource
	private JsonHandler jsonHandler;
	
	
	@RequestMapping("/listByPage")
	@ResponseBody
	public void listByPage(HttpServletResponse response,FunctionOffice entity, int pageIndex, int pageSize){
		Pagination<FunctionOffice> pagination = functionOfficeService.listByPage(entity,pageIndex,pageSize);
		jsonHandler.sendJsonString(response, true, "获取数据成功", pagination, new String[]{
			"id","name","code","createName","remark","createDate"
		}, "id","name","code","createName","remark","createDate");
	}
	
	@Override
	@RequestMapping("/add")
	@ResponseBody
	public ResponseMessage save(FunctionOffice t) {
		String code = simpleCode.generatorSimpleCode(FunctionOffice.class, "code");
		t.setCode(code);
		String message =  functionOfficeService.save(t);
		if("ok".equals(message)){
			return ResponseMessage.createMessage(true, "添加职能部门成功");
		}else{
			return ResponseMessage.createMessage(false, message);
		}
	}

	@Override
	@RequestMapping("/edit")
	@ResponseBody
	public ResponseMessage edit(FunctionOffice t) {
		FunctionOffice functionOfficed = functionOfficeService.getById(t.getId());
		t.setCode(functionOfficed.getCode());	
		String message = functionOfficeService.edit(t);
		if("ok".equals(message)){
			return ResponseMessage.createMessage(true, "修改职能部门成功");
		}else{
			return ResponseMessage.createMessage(false, message);
		}
	}

	@Override
	@RequestMapping("/del")
	@ResponseBody
	public ResponseMessage del(FunctionOffice t) {
		try{
			String message = functionOfficeService.del(t);
			if("ok".equals(message)){
				return ResponseMessage.createMessage(true, "删除职能部门成功");
			}else{
				return ResponseMessage.createMessage(false, message);
			}
		}catch(Exception e){
			return ResponseMessage.createMessage(false, "职能部门被使用");
		}
		
	}
	
	@RequestMapping("/info")
	@ResponseBody
	public void info(HttpServletResponse response,long id){
		FunctionOffice functionOffice = functionOfficeService.getById(id);
		jsonHandler.sendJsonString(response, true, "获取数据成功", functionOffice, new String[]{
				"id","name","code","createName","remark"
			}, "id","name","code","createName","remark");	
	}
	
	@RequestMapping("/listAll")
	@ResponseBody
	public void listAll(HttpServletResponse response){
		List<FunctionOffice> functionOffices = functionOfficeService.findAll();
		JsonUtils.sendJsonFromList(response, true, "获取数据成功", functionOffices, new String[]{
				"id","name","code","createName","remark"
			}, "id","name","code","createName","remark");
	}
	
	

	@RequestMapping("/listRoles")
	@ResponseBody
	public void listRoles(HttpServletResponse response,long id){
		FunctionOffice functionOffice = functionOfficeService.getById(id);
		List<SystemRole> systemRoles = functionOffice.getSystemRoles();
		JsonUtils.sendJsonFromList(response, true,"获取数据成功", systemRoles, new String[]{"id","roleName","name","code"},
				"id","roleName","name","code");
	}
	
	
	@RequestMapping("/grantRole")
	@ResponseBody
	public ResponseMessage grantRole(FunctionOffice functionOffice,String roleIds){
		if(roleIds != null){
			functionOfficeService.grantRole(functionOffice,roleIds);
		}
		return ResponseMessage.createMessage(true, "分配角色成功");
	}
}
